package com.example.transactionhintassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.transactionhintassistant.pojo.entity.Ringtone;
import com.example.transactionhintassistant.pojo.entity.RingtoneSelection;
import com.example.transactionhintassistant.pojo.vo.RingtoneVo;
import com.example.transactionhintassistant.service.RingtoneSelectionService;
import com.example.transactionhintassistant.mapper.RingtoneSelectionMapper;
import com.example.transactionhintassistant.service.RingtonesService;
import com.example.transactionhintassistant.utils.JwtHelper;
import com.example.transactionhintassistant.utils.result.Result;
import com.example.transactionhintassistant.utils.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 86147
 * @description 针对表【ringtone_selection(铃声选择表)】的数据库操作Service实现
 * @createDate 2024-03-04 22:32:13
 */
@Service
public class RingtoneSelectionServiceImpl extends ServiceImpl<RingtoneSelectionMapper, RingtoneSelection>
        implements RingtoneSelectionService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RingtonesService ringtonesService;
    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<Object> addRingtoneSelection(Integer userId) {
        LambdaQueryWrapper<RingtoneSelection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RingtoneSelection::getUserId, userId);
        if (!this.exists(queryWrapper)) {
            LambdaQueryWrapper<Ringtone> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Ringtone::getRingtoneName, "默认铃声");
            Ringtone ringtone = ringtonesService.getOne(lambdaQueryWrapper);
            if (ringtone != null) {
                RingtoneSelection ringtoneSelection = new RingtoneSelection();
                ringtoneSelection.setUserId(userId);
                ringtoneSelection.setRingtoneId(ringtone.getId());
                this.save(ringtoneSelection);
                return Result.ok(null, "添加成功！");
            }
            return Result.build(null, 500, "添加错误！");
        }
        return Result.build(null, ResultCodeEnum.ILLEGAL_OPERATION);
    }

    @Override
    public Result<Object> changeRingtoneSelection(Integer ringtoneId) {
        String token = request.getHeader("Authorization");
        Integer userId = jwtHelper.getUserId(token);
        LambdaQueryWrapper<RingtoneSelection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RingtoneSelection::getUserId, userId);
        if (this.exists(queryWrapper)) {
            if (ringtonesService.isRingtoneExists(ringtoneId)) {
                LambdaUpdateWrapper<RingtoneSelection> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(RingtoneSelection::getUserId, userId)
                        .set(RingtoneSelection::getRingtoneId, ringtoneId);
                this.update(updateWrapper);
                return Result.ok(null, "修改成功！");
            }
            return Result.build(null, 500, "铃声不存在！");
        }
        return Result.build(null, ResultCodeEnum.ILLEGAL_OPERATION);
    }

    @Override
    public Result<RingtoneVo> getSelectRingtoneByUserId(Integer userId){
        LambdaQueryWrapper<RingtoneSelection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RingtoneSelection::getUserId,userId);
        RingtoneSelection ringtoneSelection = this.getOne(queryWrapper);
        Ringtone ringtone = ringtonesService.getById(ringtoneSelection.getRingtoneId());
        RingtoneVo ringtoneVo = new RingtoneVo();
        BeanUtils.copyProperties(ringtone,ringtoneVo);
        return Result.ok(ringtoneVo);
    }
}




